package com.lyw.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyw.forum.common.enums.ResultCode;
import com.lyw.forum.common.exception.ApplicationException;
import com.lyw.forum.common.utils.BeanTransUtils;
import com.lyw.forum.mapper.BoardMapper;
import com.lyw.forum.pojo.dataobject.Board;
import com.lyw.forum.pojo.response.AppResult;
import com.lyw.forum.pojo.response.BoardSelectResponse;
import com.lyw.forum.service.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<BoardSelectResponse> selectByNum(Integer num) {
        //查询数据库信息
        // 2. 使用LambdaQueryWrapper确保类型安全
        LambdaQueryWrapper<Board> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Board::getState, 0)
                .eq(Board::getDeleteState, 0)
                .orderByAsc(Board::getSort);

        Page<Board> page = new Page<>(1, num);
        List<Board> boards = boardMapper.selectPage(page, queryWrapper).getRecords();

        List<BoardSelectResponse> boardResponses = BeanTransUtils.transBoard(boards);

        return boardResponses;
    }

    @Override
    public void addOneArticleCountById(Integer id) {
        //查询对应的板块
        Board board = boardMapper.selectById(id);

        //判断
        if (board == null) {
            //打印日志
            log.error(ResultCode.ERROR_IS_NULL.toString() + ", board id = " + id);
            //抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_IS_NULL));
        }

        //更新板块的帖子数量
        Board boardUpdate = new Board();
        boardUpdate.setId(id);
        boardUpdate.setArticleCount(board.getArticleCount() + 1);

        //更新数据库
        int row = boardMapper.updateById(boardUpdate);
        if (row != 1) {
            //打印日志
            log.error(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString() + ", 受影响的行数不等于1");
            //抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        //打印日志
        log.info("更新板块帖子数量成功, board id = " + id);
    }

    @Override
    public BoardSelectResponse selectById(Integer boardId) {

        Board board = null;
        try {
            //查询数据库
            board = boardMapper.selectById(boardId);
        } catch (Exception e) {
            log.error(ResultCode.ERROR_SERVICES.toString() + ", board id = " + boardId);
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }

        //转化
        BoardSelectResponse boardSelectResponse = BeanTransUtils.transToBoardSelectResponse(board);

        return boardSelectResponse;
    }

    @Override
    public void subOneArticleCountById(Integer id) {
        //查询板块详情
        Board board = boardMapper.selectById(id);

        if (board == null) {
            //打印日志
            log.error(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ", board id = " + id);
            //抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        //构造更新对象
        Board boardUpdate = new Board();
        boardUpdate.setId(id);
        boardUpdate.setArticleCount(board.getArticleCount() - 1);
        boardUpdate.setUpdateTime(new Date());

        //判断减1之后是否小于0
        if (boardUpdate.getArticleCount() < 0) {
            //如果小于0则设置为0
            boardUpdate.setArticleCount(0);
        }

        //调用mapper
        int row = 0;
        try {
            row = boardMapper.updateById(boardUpdate);
        } catch (Exception e) {
            //打印日志
            log.error(ResultCode.ERROR_SERVICES.toString() + ", board id = " + id);
            //抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }

        if (row != 1) {
            //打印日志
            log.error(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString() + ", 受影响的行数不等于1");
            //抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
    }
}
